﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using BacteriaSyatem.FloatToolBar;

namespace FileMd5Checker
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                tbFileName.Text = openFileDialog1.FileName;
            }
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            string readyString = tbString.Text.Trim();
            string readyFile = tbFileName.Text.Trim();
            string md5Value = "";
            if (readyString!="")
            {
                md5Value = CaculateMd5.GetMD5FromString(readyString);
            }
            else
            {
                if (readyFile!="")
                {
                    lbState.Text = "正在计算文件Md5值，计算时间长短和文件大小有关，请等待...";
                    btnCalculate.Enabled = btnOpenFile.Enabled = btnCompare.Enabled = false;
                    Thread t = new Thread(() =>
                    {
                        md5Value = CaculateMd5.GetMD5FromFile(readyFile);
                        this.UIInvoke(() =>
                        {
                            lbState.Text = "计算完成";
                            tbMd5.Text = md5Value;
                            btnCalculate.Enabled = btnOpenFile.Enabled = btnCompare.Enabled = true;
                        });
                    }){IsBackground = true,Name = "计算文件Md5"};
                    t.Start();
                }
            }
            tbMd5.Text = md5Value;
        }

        private void btnCompare_Click(object sender, EventArgs e)
        {
            lbRlt.Text = "";
            string md51 = tbMd5Source.Text.Trim().ToUpper();
            string md52 = tbMd5.Text.Trim().ToUpper();
            if (md51==""||md52=="")
            {
                return;
            }

            if (string.CompareOrdinal(md51,md52)==0)
            {
                lbRlt.Text = "匹配成功";
                lbRlt.ForeColor = Color.LightSeaGreen;
            }
            else
            {
                lbRlt.Text = "匹配失败";
                lbRlt.ForeColor = Color.Tomato;
            }
        }
    }
}
